package co.yixiang.modules.goods.serviceimpl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.goods.entity.domain.YxStoreProductUnit;
import co.yixiang.modules.goods.entity.dto.YxStoreProductUnitDto;
import co.yixiang.modules.goods.entity.dto.YxStoreProductUnitQueryCriteria;
import co.yixiang.modules.goods.mapper.StoreProductUnitMapper;
import co.yixiang.modules.goods.service.YxStoreProductUnitService;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxStoreProduct")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreProductUnitServiceImpl extends BaseServiceImpl<StoreProductUnitMapper, YxStoreProductUnit> implements YxStoreProductUnitService {

    private final IGenerator generator;

    @Override
    public Map<String, Object> queryAll(YxStoreProductUnitQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxStoreProductUnit> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxStoreProductUnitDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<YxStoreProductUnit> queryAll(YxStoreProductUnitQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxStoreProductUnit.class, criteria));
    }
}
